<?php
class Page_m extends MY_Model{
	
	private $_table_lang = 'page_article_lang';
	private $select = 'p.id, p.created, p.create_by, p.parent_id, p._class, p.ordering, l.title, l.slug, l.keywords, l.description,l.uri';
	private $select_full = 'p.id, p.created, p.create_by, p.parent_id, p._class, l.title, l.slug, l.keywords, l.description, l.bodyhtml,l.uri';
	
	
	function __construct(){
		parent::__construct();
		$this->_table = 'page_article';
	}
	
	public function get_all(){
		$this->_table = 'page_article p';
		$this->_table_lang = 'page_article_lang l';
		$this->db->select($this->select);
		$this->db->join($this->_table_lang,'p.id = l.id');
		$this->db->where('l.lang',$this->_lang);
		return $this->db->get($this->_table)->result();
		
	}
	
	public function insert($data){
		//step1
		$page['parent_id'] = $data['parent_id'];
		$page['_class'] = $data['_class'];
		$page['created'] = strtotime(date('Y-m-d H:i'));
		$page['create_by'] = $this->current_user->id;
		$id_insert = parent::insert($page);
		//step2
		$this->_table = $this->_table_lang;
		if(isset($data['slug'])){
			$check = $this->db->where('slug',$data['slug'],'lang',$this->_lang)->get($this->_table)->row();
			if(count($check)>0)
				$data['slug'] .= "-".time();
		}
		else{
			$data['slug'] .= "-".time();
		}
		$page_lang['id'] = $id_insert;
		$page_lang['title'] = $data['title'];
		$page_lang['slug'] = $data['slug'];
		$page_lang['keywords'] = $data['keywords'];
		$page_lang['description'] = $data['description'];
		$page_lang['bodyhtml'] = $data['bodyhtml'];
		$page_lang['uri'] = $data['uri'];
		foreach($this->_lang_data as $l){
			$page_lang['lang'] = $l;
			parent::insert($page_lang);
		}
		
		return $id_insert;
	}
	
	public function update($id,$data){
		//step1
		$this->_table = 'page_article';
		$page['parent_id'] = $data['parent_id'];
		$page['_class'] = $data['_class'];
		parent::update($id,$page);
		unset($data['parent_id']);
		unset($data['_class']);
		//step2
		$this->_table = $this->_table_lang;
		if(isset($data['slug'])){
			$check = $this->db->where_not_in('id', $id)->where('slug',$data['slug'])->get($this->_table)->row();
			if(count($check)>0)
				$data['slug'] .= "-".time();
		}
		else{
			$data['slug'] .= "-".time();
		}
		$this->db->where('l.lang',$this->_lang);
	//	print_r($data);exit;
		parent::update($id,$data);
	}
	
	public function get($id){
		$this->_table .= ' p';
		$this->_table_lang .= ' l';
		$this->db->select($this->select_full);
		$this->db->join($this->_table_lang,'p.id = l.id');
		$this->db->where('p.id',$id);
		$this->db->where('l.lang',$this->_lang);
		return $this->db->get($this->_table)->row();
	}
	
	//list de quy
	public function page_list($parent_id = 0, $sym = ''){
		$this->_table = 'page_article p';
		$this->_table_lang = 'page_article_lang l';
		$this->db->select($this->select);
		$this->db->join($this->_table_lang,'p.id = l.id');
		$this->db->where('p.parent_id',$parent_id);
		$this->db->where('l.lang',$this->_lang);
		$this->db->order_by('ordering');
		
		$result = $this->db->get($this->_table)->result();
		
		foreach($result as $item){
			$item->frefix = $sym;
			$item->child = $this->page_list($item->id,$sym.'►');
		}
		//print_r($result);exit;
		return $result;
	}
	
	public function delete($id){
		$this->_table = 'page_article';
		parent::delete($id);
		//update cho child co parent_id
		$this->db->where('parent_id',$id);//old
		$this->db->update($this->_table,array('parent_id'=>$parent_id));
		
		$this->_table = 'page_article_lang';
		parent::delete($id);
		
	}
	
	function ordering( $id, $model ){
		$this->_table = 'page_article';
		return parent::update($id,$model);
	}
}